package com.evil.common.core.entity;

import jakarta.validation.constraints.NotBlank;
import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

@Data
public final class Order implements Serializable {
    @NotBlank(message = "排序字段错误")
    private String field;
    private boolean desc;

    public Order() {
    }

    public Order(String field, boolean desc) {
        this.field = field;
        this.desc = desc;
    }

    public static Order asc(String field) {
        return new Order(field, false);
    }

    public static Order desc(String field) {
        return new Order(field, true);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Order order = (Order) o;
        return Objects.equals(field, order.field);
    }

    @Override
    public int hashCode() {
        return Objects.hash(field);
    }

    @Override
    public String toString() {
        return String.format("%s %s", this.field, this.desc ? "DESC" : "ASC");
    }
}
